<?php

namespace Biz\model;

use Doraemon\tools\ImgUrl;

/**
 *
 * @author lee
 *
 */
class ShopSecurityDepositLog extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_security_deposit_log';

    const _SUB_TABLE = false;


    //原因分类
    const TYPE_SHOP_PAY_DEPOSIT = 1;//店铺交保
    const TYPE_SHOP_VIOLATION = 2;//店铺违规
    const TYPE_SHOP_ADMIN_SET_AMOUNT = 3;//管理员设置保证金额度
    const TYPE_SHOP_ADD_GOODS_AMOUNT = 4;//保证金补货款
    const TYPE_SHOP_CLOSE_BACK_AMOUNT = 5;//关店退保

    //状态
    const STATUS_NORMAL = 1;//待处理
    const STATUS_PROCESSED = 2;//已处理

    //店铺交保（正数）
    const REASON_OPEN = 1;//好好住商城开店保证金
    const REASON_WITHHOLD_AMOUNT = 2;//补缴保证金-店铺违规扣款
    const REASON_ADDITIONAL_AMOUNT = 3;//补缴保证金-补缴负数货款
    const REASON_WITHHOLD_AND_ADDITIONAL_AMOUNT = 4;//补缴保证金-店铺违规扣款及补缴负数货款

    //违规扣款（负数）
    const REASON_WITHHOLD_DEPOSIT = 5;//店铺违规扣保证金

    //保证金转货款
    const REASON_DEPOSIT_ADD_GOODS_AMOUNT = 6;//保证金抵负数货款

    //关闭店铺
    const REASON_CLOSE = 7;

    public static $typeDesc = [
        self::TYPE_SHOP_PAY_DEPOSIT => '店铺交保',
        self::TYPE_SHOP_VIOLATION => '店铺违规',
        self::TYPE_SHOP_ADMIN_SET_AMOUNT => '管理员操作',
        self::TYPE_SHOP_ADD_GOODS_AMOUNT => '保证金补货款',
        self::TYPE_SHOP_CLOSE_BACK_AMOUNT => '关店退保',
    ];

    public static $typeAdmOperatDesc = [
        self::TYPE_SHOP_PAY_DEPOSIT => '店铺交保',
        self::TYPE_SHOP_VIOLATION => '店铺违规',
        self::TYPE_SHOP_CLOSE_BACK_AMOUNT => '关闭店铺',
    ];

    public static $statusDesc = [
        self::STATUS_NORMAL => '待处理',
        self::STATUS_PROCESSED => '已处理'
    ];

    public static $reasonDesc = [
        //店铺交保（正数）
        self::REASON_OPEN => "好好住商城开店保证金",
        self::REASON_WITHHOLD_AMOUNT => "补缴保证金-店铺违规扣款",
        self::REASON_ADDITIONAL_AMOUNT => "补缴保证金-补缴负数货款",
        self::REASON_WITHHOLD_AND_ADDITIONAL_AMOUNT => "补缴保证金-店铺违规扣款及补缴负数货款",
        //违规扣款（负数）
        self::REASON_WITHHOLD_DEPOSIT => "店铺违规扣保证金",
        //保证金转货款
        self::REASON_DEPOSIT_ADD_GOODS_AMOUNT => "保证金抵负数货款",
        //关闭店铺
        self::REASON_CLOSE => '好好住商城关店退保证金',
    ];


    public static function insertLog($uid, $data)
    {

        $data['uid'] = $uid;

        $data['shop_id'] = $uid;


        $data['addtime'] = date('Y-m-d H:i:s');


        $ret = self::insert($data);

        if ($ret->errorCode() !== '00000') {
            throw new \Exception('数据库操作错误');
        }

        return self::id();

    }

    public static function getLog($id)
    {
        $res = self::get("*", ['id' => $id]);
        return $res;

    }

    public static function getLogList($ids)
    {
        $res = self::select("*", ['id' => $ids, 'ORDER' => ['id' => 'DESC']]);

        if ($res) {
            foreach ($res as &$value) {
                self::formatLog($value);
            }

        }
        return $res;

    }

    public static function getLogs($params, $limit, $order, $checkImgStatus = false)
    {
        $where['AND'] = $params;
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        if ($order) {
            $where['ORDER'] = $order;
        }
        $res = self::select('*', $where);
        if ($res) {
            foreach ($res as &$value) {
                self::formatLog($value, $checkImgStatus);
            }

        }
        return $res;
    }

    public static function getLogsCount($where)
    {
        return self::count('*', $where);
    }

    public static function hasTransNumber($transNumber)
    {
        return self::has(['trans_number' => $transNumber]);
    }


    public static function formatLog(&$log, $checkImgStatus = false)
    {
        if (empty($log)) return;

        foreach ($log as $key => &$value) {
            switch ($key) {
                case 'type':
                    $log['type_desc'] = isset(self::$typeDesc[$value]) ? self::$typeDesc[$value] : '其他类型';
                    break;
                case 'status':
                    $log['status_desc'] = isset(self::$statusDesc[$value]) ? self::$statusDesc[$value] : '';
                    break;
                case 'img':
                    if ($checkImgStatus) {
                        $log['img_url'] = ($value && $log['img_status']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $value, ImgUrl::IMAGE_SIZE_SQ_PHONE) : '';
                    } else {
                        $log['img_url'] = $value ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $value, ImgUrl::IMAGE_SIZE_SQ_PHONE) : '';
                    }
            }
        }
    }


}

